package com.daiyang.lotterysystem.service.Impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.daiyang.lotterysystem.common.errorcode.ServiceErrorCodeConstants;
import com.daiyang.lotterysystem.common.errorexception.ServiceException;
import com.daiyang.lotterysystem.common.utils.RegexUtil;
import com.daiyang.lotterysystem.controller.param.UserRegisterParam;
import com.daiyang.lotterysystem.dao.dataobject.Encrypt;
import com.daiyang.lotterysystem.dao.dataobject.UserDO;
import com.daiyang.lotterysystem.dao.mapper.UserMapper;
import com.daiyang.lotterysystem.service.UserService;
import com.daiyang.lotterysystem.service.dto.UserRegisterDTO;
import com.daiyang.lotterysystem.service.enums.UserIdentityEnum;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl implements UserService {
    @Resource(name="userMapper")
    private UserMapper userMapper;
    @Override
    public UserRegisterDTO register(UserRegisterParam Param) {
        //校验注册信息
        checkRegisterInfo(Param);
        //加密私密数据
        UserDO userDO = new UserDO();
        userDO.setUserName(Param.getName());
        userDO.setIdentity(Param.getIdentity());
        userDO.setPhoneNumber(new Encrypt(Param.getPhoneNumber()));
        userDO.setEmail(Param.getMail());
        if(StringUtils.hasText(Param.getPassword())){
            userDO.setPassword(DigestUtil.sha256Hex(Param.getPassword()));
        }
        //保存数据
        userMapper.insert(userDO);

        //发送注册邮件

        //发送注册短信
        //构造返回
        UserRegisterDTO userRegisterDTO = new UserRegisterDTO();
        userRegisterDTO.setUserId(userDO.getId());
        return userRegisterDTO;
    }
    private void checkRegisterInfo(UserRegisterParam param){
        if(null==param){

            throw new ServiceException(ServiceErrorCodeConstants.RESGISTER_INFO_IS_EMPTY);
        }

        //校验邮箱格式xx@xxx.xxx

        if(RegexUtil.checkMail(param.getMail())) {
            throw new ServiceException(ServiceErrorCodeConstants.MAIL_ERROR);
        }


        //校验手机号格式
       if(!RegexUtil.checkMobile(param.getPhoneNumber())) {
           throw new ServiceException(ServiceErrorCodeConstants.PHONE_NUMBER_ERROR);
       }
        //校验身份证格式
        if(null== UserIdentityEnum.forName(param.getIdentity())){
            throw new ServiceException(ServiceErrorCodeConstants.IDENTITY_ERROR);
        }

        //校验管理员密码必填
        if (param.getIdentity().equals(UserIdentityEnum.ADMIN.name())&&
                !StringUtils.hasText(param.getPassword())) {
            throw new ServiceException(ServiceErrorCodeConstants.PASSWORD_IS_EMPTY);
        }

        //密码校验，至少6位
        if(!StringUtils.hasLength(param.getPassword())
                && !RegexUtil.checkPassword(param.getPassword())){
            throw new ServiceException(ServiceErrorCodeConstants.PASSWORD_ERROR);

        }

        //校验邮箱是否使用
        if(cheackMailIsUsed(param.getMail())){
            throw new ServiceException(ServiceErrorCodeConstants.MAIL_USED);
        }

        //校验手机号是否使用
        if(cheackPhoneNumberUsedUsed(param.getPhoneNumber())) {
            throw new ServiceException(ServiceErrorCodeConstants.PHONE_NUMBER_USED);
        }



    }
    private boolean cheackPhoneNumberUsedUsed( String phoneNumber) {
        int count=userMapper.countPhoneNumber(new Encrypt(phoneNumber));
        return count>0;
    }
    private boolean cheackMailIsUsed(@NotBlank(message = "邮箱不能为空") String email) {
        return  userMapper.countEmail(email)>0;
    }
}
